class_name LoadingScreen extends CanvasLayer

#加载全屏
signal  loading_screen_has_full_coverage

@onready var animationPlay : AnimationPlayer = $AnimationPlayer
@onready var progressBar: ProgressBar = $ProgressPanel/ProgressBar
@onready var PanelWindows: Panel = $ProgressPanel


func _ready():
	
	#设置窗口面板大小
	var window_size = get_viewport().size  # 获取窗口尺寸
	print(window_size)
	# 调整 Panel 的大小
	PanelWindows.set_size(window_size)

	
	updateLoadAnimation('start_loading',0,0,0, window_size.x)
	updateLoadAnimation('end_loading',0,1,0, window_size.x)



## animationName动画名称  ptrack_idx 轨道索引
func updateLoadAnimation(animationName:String,track_index:int,keyIndex:int,valueIndex:int,value:int)->void:
		#动画出现窗口位置 ,修改开始结束动画的x位置自适应
	var loadingAnimation =  animationPlay.get_animation(animationName)

	# 打印所有轨道的名称
	# 打印所有轨道的类型和目标路径
	#for i in range(startLoadingAnimation.get_track_count()):
		#var track_type = startLoadingAnimation.track_get_type(i)
		#var node_path = startLoadingAnimation.track_get_path(i)  # 获取与轨道关联的节点路径
		#print("轨道索引:", i, "类型:", track_type, "节点路径:", node_path)
 
	# 获取轨道索引
	var track_idx = loadingAnimation.find_track(loadingAnimation.track_get_path(track_index), loadingAnimation.track_get_type(track_index))
	# 设置新的关键帧值
	var oldValue = loadingAnimation.track_get_key_value(track_idx,keyIndex)
	oldValue[valueIndex] = value
	#设置开始动画0帧位置
	loadingAnimation.track_set_key_value(track_idx, keyIndex,oldValue)
   
	

## 更新加载进度  
func _update_progress_bar(new_value: float) -> void:
	progressBar.set_value_no_signal(new_value * 100)
	

## 加载结束开始另一个动画
func  _start_outro_animation() -> void:
	await  Signal(animationPlay,"animation_finished")
	animationPlay.play("end_loading")
	await  Signal(animationPlay,"animation_finished")
	self.queue_free()
	
